package com.hkfn.rfb.rfb.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.hkfn.rfb.MainActivity;
import com.hkfn.rfb.R;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.bean.ConstData;
import com.hkfn.rfb.common.fragment.BaseFragment;
import com.hkfn.rfb.databinding.FRfbMainBinding;

/**
 *  RFB主页页面
 */
public class RfbMainFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private FRfbMainBinding fRfbMainBinding;

    private BaseFragment mCurrentBaseFragment;
    private int currentPage = -1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getMyActivity();
        bundle = getArguments();
        fRfbMainBinding = FRfbMainBinding.inflate(inflater, container, false);
        initView();
        return fRfbMainBinding.getRoot();
    }

    private void initView() {
        fRfbMainBinding.includeRfbSystemOverview.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_overview);
        fRfbMainBinding.includeRfbSystemOverview.tvItemTitle.setText("系统总览");
        fRfbMainBinding.includeRfbDataMonitor.ivItemImg.setImageResource(R.mipmap.icon_rfb_data_monitor);
        fRfbMainBinding.includeRfbDataMonitor.tvItemTitle.setText("数据监控");
        fRfbMainBinding.includeRfbTrendSummary.ivItemImg.setImageResource(R.mipmap.icon_rfb_trend_summary);
        fRfbMainBinding.includeRfbTrendSummary.tvItemTitle.setText("分析汇总");
        fRfbMainBinding.includeRfbProjectManage.ivItemImg.setImageResource(R.mipmap.icon_rfb_project_manage);
        fRfbMainBinding.includeRfbProjectManage.tvItemTitle.setText("项目管理");
        fRfbMainBinding.includeRfbSystemOperation.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_operation);
        fRfbMainBinding.includeRfbSystemOperation.tvItemTitle.setText("系统运维");
        fRfbMainBinding.includeRfbSystemSetting.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_setting);
        fRfbMainBinding.includeRfbSystemSetting.tvItemTitle.setText("系统配置");

        fRfbMainBinding.includeRfbSystemOverview.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(0);
            }
        });
        fRfbMainBinding.includeRfbDataMonitor.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(1);
            }
        });
        fRfbMainBinding.includeRfbTrendSummary.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(2);
            }
        });
        fRfbMainBinding.includeRfbProjectManage.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(3);
            }
        });
        fRfbMainBinding.includeRfbSystemOperation.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(4);
            }
        });
        fRfbMainBinding.includeRfbSystemSetting.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmentShow(5);
            }
        });

        changeFragmentShow(0);
    }

    private void changeFragmentShow(int setPage){
        if (currentPage != setPage){
            switch (currentPage){
                case 0:
                    fRfbMainBinding.includeRfbSystemOverview.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_overview);
                    fRfbMainBinding.includeRfbSystemOverview.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbSystemOverview.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;
                case 1:
                    fRfbMainBinding.includeRfbDataMonitor.ivItemImg.setImageResource(R.mipmap.icon_rfb_data_monitor);
                    fRfbMainBinding.includeRfbDataMonitor.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbDataMonitor.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;
                case 2:
                    fRfbMainBinding.includeRfbTrendSummary.ivItemImg.setImageResource(R.mipmap.icon_rfb_trend_summary);
                    fRfbMainBinding.includeRfbTrendSummary.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbTrendSummary.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;
                case 3:
                    fRfbMainBinding.includeRfbProjectManage.ivItemImg.setImageResource(R.mipmap.icon_rfb_project_manage);
                    fRfbMainBinding.includeRfbProjectManage.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbProjectManage.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;
                case 4:
                    fRfbMainBinding.includeRfbSystemOperation.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_operation);
                    fRfbMainBinding.includeRfbSystemOperation.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbSystemOperation.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;
                case 5:
                    fRfbMainBinding.includeRfbSystemSetting.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_setting);
                    fRfbMainBinding.includeRfbSystemSetting.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.rfb_left_unselected_color));
                    fRfbMainBinding.includeRfbSystemSetting.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.transparent));
                    break;

            }
            currentPage = setPage;
            BaseFragment baseFragment = null;
            switch (currentPage){
                case 0:
                    fRfbMainBinding.includeRfbSystemOverview.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_overview_highlight);
                    fRfbMainBinding.includeRfbSystemOverview.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbSystemOverview.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbSystemOverviewFragment();
                    break;
                case 1:
                    fRfbMainBinding.includeRfbDataMonitor.ivItemImg.setImageResource(R.mipmap.icon_rfb_data_monitor_highlight);
                    fRfbMainBinding.includeRfbDataMonitor.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbDataMonitor.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbDataMonitorFragment();
                    break;
                case 2:
                    fRfbMainBinding.includeRfbTrendSummary.ivItemImg.setImageResource(R.mipmap.icon_rfb_trend_summary_highlight);
                    fRfbMainBinding.includeRfbTrendSummary.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbTrendSummary.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbTrendSummaryFragment();
                    break;
                case 3:
                    fRfbMainBinding.includeRfbProjectManage.ivItemImg.setImageResource(R.mipmap.icon_rfb_project_manage_highlight);
                    fRfbMainBinding.includeRfbProjectManage.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbProjectManage.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbProjectManageFragment();
                    break;
                case 4:
                    fRfbMainBinding.includeRfbSystemOperation.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_operation_highlight);
                    fRfbMainBinding.includeRfbSystemOperation.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbSystemOperation.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbSystemOperationFragment();
                    break;
                case 5:
                    fRfbMainBinding.includeRfbSystemSetting.ivItemImg.setImageResource(R.mipmap.icon_rfb_system_setting_highlight);
                    fRfbMainBinding.includeRfbSystemSetting.tvItemTitle.setTextColor(activity.getResources().getColor(R.color.white));
                    fRfbMainBinding.includeRfbSystemSetting.getRoot().setBackgroundColor(activity.getResources().getColor(R.color.rfb_left_selected_bg_color));
                    baseFragment = new RfbSystemSettingFragment();
                    break;

            }
            addFragment(fRfbMainBinding.flRfbContainer.getId(), baseFragment);
        }
    }

    /**
     * 添加fragment到activity
     * @param viewId fragment填充的位置
     * @param baseFragment 填充的对象
     */
    protected void addFragment(int viewId, BaseFragment baseFragment) {
        try {
            if(null == baseFragment || baseFragment.equals(mCurrentBaseFragment)) {
                return;
            }

            FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();

            // 隐藏已显示的Fragment
            if (null != mCurrentBaseFragment) {
                fragmentTransaction.hide(mCurrentBaseFragment);
            }

            if (baseFragment.isAdded()) {
                fragmentTransaction.show(baseFragment);
            }
            else {
                fragmentTransaction.add(viewId, baseFragment, baseFragment.getClass().getSimpleName());
            }
            fragmentTransaction.commitAllowingStateLoss();
            mCurrentBaseFragment = baseFragment;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ((MainActivity) activity).setMenuTitle(ConstData.FUNCTION_MENU);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fRfbMainBinding = null;
    }

    @Override
    public void updateViewData() {
        super.updateViewData();
        if (null != mCurrentBaseFragment){
            mCurrentBaseFragment.updateViewData();
        }
    }
}
